﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;
using System.Collections;
using System.Data;

namespace Web.SetSystem
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {

            if (!((Account)HttpContext.Current.Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");
            if (!IsPostBack)
            {
                //绑定班级DropDownList
                ArrayList arraylist = new ArrayList();
                arraylist = Library.GetClassLib();
                for (int i = 0; i < arraylist.Count; i++)
                {
                    ListItem listitem = new ListItem();
                    listitem = (ListItem)arraylist[i];
                    DropDownList_ClassInfo1.Items.Add(listitem);
                }

                //绑定答辩场次信息DropDownList
                ArrayList arraylistDef = new ArrayList();
                //获取该年所有答辩场次ID
                arraylistDef = Defense.GetDefenseListByYear(DeadLine.GetDeadLineYear());
                for (int i = 0; i < arraylistDef.Count; i++)
                {
                    ListItem myListItem = new ListItem();
                    myListItem.Value = arraylistDef[i].ToString();
                    myListItem.Text = Defense.GetDefenseName(decimal.Parse(myListItem.Value));
                    DropDownList_DefenseInfo1.Items.Add(myListItem);
                }
            }
        }

        //选择班级绑定学生列表
        protected void DropDownList_ClassInfo1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //所选班级的学生DataSet
            ClassInfo myClassInfo = new ClassInfo(DropDownList_ClassInfo1.SelectedItem.Text.ToString());
            DataSet ds = myClassInfo.GetStudentsDataSet();
            //拼接的DataSet
            DataSet ds2 = new DataSet();
            DataTable dt = new DataTable();
            ds2.Tables.Add(dt);
            dt.Columns.Add("stdid");
            dt.Columns.Add("stdname");
            dt.Columns.Add("defenseinfo");


            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //某学生的答辩信息DataSet
                DataSet ds1 = new DataSet();
                ds1 = Defense.GetDefenseList(ds.Tables[0].Rows[i][0].ToString());

                DataRow dr = dt.NewRow();
                dr["stdid"] = ds.Tables[0].Rows[i]["std_id"].ToString();
                dr["stdname"] = ds.Tables[0].Rows[i]["std_name"].ToString();
                dr["defenseinfo"] = "时间："+ds1.Tables[0].Rows[0]["def_time"].ToString() +"地点： "+ ds1.Tables[0].Rows[0]["def_room"].ToString();
                dt.Rows.Add(dr);
            }
            
            //绑定自定义DataSet
            GridView_StuList.DataSource = ds2; 
            GridView_StuList.DataBind();
        }
        //为CheckBox选中学生添加答辩场次信息
        protected void ImageButton_StuAdd_Click(object sender, ImageClickEventArgs e)
        {
            for (int i = 0; i < GridView_StuList.Rows.Count - 1; i++)
            {
                CheckBox checkbox = (CheckBox)GridView_StuList.Rows[i].FindControl("CheckBox1");
                if (checkbox.Checked)
                {
                    Student myStudent = new Student(GridView_StuList.Rows[i].Cells[0].Text.ToString());
                    Label_StuDefenseResult.Text = myStudent.AddDefense(decimal.Parse(DropDownList_DefenseInfo1.SelectedItem.Value.ToString()));
                }
            }
        }

        protected void GridView_StuList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_StuList.PageIndex = e.NewPageIndex;
            //所选班级的学生DataSet
            ClassInfo myClassInfo = new ClassInfo(DropDownList_ClassInfo1.SelectedItem.Text.ToString());
            DataSet ds = myClassInfo.GetStudentsDataSet();
            //拼接的DataSet
            DataSet ds2 = new DataSet();
            DataTable dt = new DataTable();
            ds2.Tables.Add(dt);
            dt.Columns.Add("stdid");
            dt.Columns.Add("stdname");
            dt.Columns.Add("defenseinfo");


            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                //某学生的答辩信息DataSet
                DataSet ds1 = new DataSet();
                ds1 = Defense.GetDefenseList(ds.Tables[0].Rows[i][0].ToString());

                DataRow dr = dt.NewRow();
                dr["stdid"] = ds.Tables[0].Rows[i]["std_id"].ToString();
                dr["stdname"] = ds.Tables[0].Rows[i]["std_name"].ToString();
                dr["defenseinfo"] = "时间：" + ds1.Tables[0].Rows[0]["def_time"].ToString() + "地点： " + ds1.Tables[0].Rows[0]["def_room"].ToString();
                dt.Rows.Add(dr);
            }

            //绑定自定义DataSet
            GridView_StuList.DataSource = ds2;
            GridView_StuList.DataBind();
        }
    }
}
